<?php
/**
 * Created by Silent Night.
 * Author: Silent Night
 * email: silentwolf_wp@163.com
 * Date: 2017/9/28
 * Time: 9:19
 */

namespace app\admin\controller;

use think\Controller;
use think\Db;
use think\Session;

class Coupon extends Controller
{
    private $_model;
    private $title_field = [
    ];
    public $pagesize = 10;

    public function _initialize()
    {
        $this->_model = new \app\admin\model\Coupon();
        $this->assign('LoginUserInfo', $this->LoginUserInfo);
        //获取当前用户的权限信息
        $this->assign('ruleLists', $this->LoginUserInfo['CurrPower']);
    }

    public function shangData()
    {
        $return = [];
        $id = trim(input("request.id"));//auth
        if (empty($id)) {
            $return = ['err' => 1, 'msg' => '操作失败'];
            return json($return);
        }
        $res = $this->_model->updateData(array('stats' => 0), $id);
        if ($res || $res === 0) {
            $return = ['err' => 0, 'msg' => '操作成功'];
            return json($return);
        } else {
            $return = ['err' => 3, 'msg' => '操作失败'];
            return json($return);
        }
    }

    public function xiaData()
    {
        $return = [];
        $id = trim(input("request.id"));//auth
        if (empty($id)) {
            $return = ['err' => 1, 'msg' => '操作失败'];
            return json($return);
        }
        $res = $this->_model->updateData(array('stats' => 1), $id);

        if ($res || $res === 0) {
            $return = ['err' => 0, 'msg' => '操作成功'];
            return json($return);
        } else {
            $return = ['err' => 3, 'msg' => '操作失败'];
            return json($return);
        }
    }

    /**
     * 列表页
     * @return mixed
     */
    public function index()
    {
        return $this->fetch('index');
    }

    public function lists()
    {
        $ser_key = trim(input("request.ser_key"));//auth
        $ser_val = trim(input("request.ser_val"));//auth
        $ser_type = trim(input("request.ser_type"));//auth
        $ser_stats = trim(input("request.ser_stats"));//auth

        if ($ser_key && $ser_val) {
            $where[$ser_key] = array('like', '%' . $ser_val . '%');
        }
        if ($ser_type > 0) {
            $where['type'] = $ser_type;
        }
        if ($ser_stats == '0' || $ser_stats == '1') {
            $where['stats'] = $ser_stats;
        }
        $where['is_delete'] = '0';
        $lists = $this->_model->lists($where, $this->pagesize, "*", "id desc");
        $lists['data'] = changeUidToUserinfoForLists($lists['data'], 'uid');
        $lists['data'] = (new \app\admin\mapper\Coupon())->listMapper($lists['data']);
        $this->assign('lists', $lists);
        $this->assign('request', input("request."));
        return view('lists');
    }

    /**
     * 删除
     * 支持批量删除和单条删除
     * @return mixed
     */
    public function delData()
    {
        $return = [];
        $id = trim(input("request.id"));//auth
        if (empty($id)) {
            $return = ['err' => 1, 'msg' => '操作失败'];
            return json($return);
        }
        $res = $this->_model->updateData(array('is_delete' => 1), $id);
        if ($res) {
            $return = ['err' => 0, 'msg' => '操作成功'];
            return json($return);
        } else {
            $return = ['err' => 3, 'msg' => '操作失败'];
            return json($return);
        }

    }


    public function addData()
    {
        $sys_opt = trim(input("request.sys_opt"));//auth
        if ($sys_opt) {
            $data1 = $_REQUEST['data'];
            $data2 = array(
                'uid' => $this->LoginUserInfo['UserInfo']['id'],
                'ctime' => time(),
            );
            $num = $data1['num'];
            if ($num <= 0) {
                $return = ['err' => 3, 'msg' => '优惠券数量不正确'];
                return json($return);
            }
            $data = array_merge($data1, $data2);
            $res = $this->_model->add($data);
            if ($res) {
                $this->addJuan($num, $res);
                $return = ['err' => 0, 'msg' => '操作成功'];
                return json($return);
            } else {
                $return = ['err' => 3, 'msg' => '操作失败'];
                return json($return);
            }
        }
        $title = "添加";
        $this->assign('title', $title);
        return view('addData');
    }

    function getNo($len)
    {
        return strtolower(str_rand($len));
    }

    private function addJuan($num, $pid)
    {
        if (empty($num)) {
            return 0;
        }
        $model = new \app\admin\model\CouponItem();
        $data = array();
        $time = time();
        for ($i = 1; $i <= $num; $i++) {
            $data[] = array(
                'pid' => $pid,
                'no' => $this->getNo(8),
                'ctime' => $time
            );
        }
        $model->addAll($data);
    }


    public function editData()
    {

        $id = trim(input("request.id"));//auth
        $sys_opt = trim(input("request.sys_opt"));//auth
        //查询用户
        $info = $this->_model->getDataForWhere(array('id' => $id));

        if ($sys_opt) {
            $data1 = $_REQUEST['data'];

            $w = array(
                'id' => $id
            );
            $data2 = array();
            $data = array_merge($data1, $data2);
            $res = $this->_model->updateDataForWhere($data, $w);

            if ($res || ($res === 0)) {

                $return = ['err' => 0, 'msg' => '操作成功'];
                return json($return);
            } else {
                $return = ['err' => 3, 'msg' => '操作失败'];
                return json($return);
            }
        }

        $this->assign('info', $info);
        //设置全局信息
        $title = "编辑";
        $this->assign('title', $title);
        $this->assign('id', $id);
        return view('editData');
    }

    public function juanLists()
    {
        $pid = trim(input("request.pid"));//auth
        $ser_is_use = trim(input("request.ser_is_use"));//auth
        $ser_is_lingque = trim(input("request.ser_is_lingque"));//auth


        if ($ser_is_use == '0' || $ser_is_use == '1') {
            $where['is_use'] = $ser_is_use;
        }

        if ($ser_is_lingque == '0' || $ser_is_lingque == '1') {
            $where['is_lingque'] = $ser_is_lingque;
        }

        $where['pid'] = $pid;
        $model = new \app\admin\model\CouponItem();
        $lists = $model->lists($where, $this->pagesize);
        $lists['data'] = changeUidToUserinfoForLists($lists['data'], 'uid');

        $this->assign('lists', $lists);
        $this->assign('pid', $pid);
        $this->assign('request', input("request."));
        return view('juanLists');
    }
}